public virtual class BaseClient {
 
	protected AtomService atomService; 
 
	public BaseClient(AtomService atomService) { 
		this.atomService = atomService;
	}
  
	public AtomService getAtomService() {
		return atomService; 
	}
	 
	public Feed getFeed_FQ(String path) {
		try {
			return (Feed)atomService.getFeed(path, true);
		} catch (Exception e) {
			System.debug(LoggingLevel.ERROR, '\n\nERROR GETFEED(' + path + ')\n' + e.getMessage());
		}
		return null;
	}
	public AtomBase getAtomBase(String path) {
		try {
			return atomService.getFeed(path, false);
		} catch (Exception e) {
			System.debug(LoggingLevel.ERROR, '\n\nERROR GETATOMBASE(' + path + ')\n' + e.getMessage());
		}
		return null;
	}
		
	public Feed getFeed(String path) {
		try {
			return (Feed)atomService.getFeed(path, false);
		} catch (Exception e) {
			System.debug(LoggingLevel.ERROR, '\n\nERROR GETFEED(' + path + ')\n' + e.getMessage());
		}
		return null;
	}
	
	protected BluehouseService makeServiceCall(String path) {
		return (BluehouseService)atomService.makeServiceCall(path);
	} 
	 
	protected List<ActivityNode> getNodes(String path, String common){
		try {
			System.debug(LoggingLevel.FINE, 'Calling atomService.getFeed(' + path + ') to get feed.');
			System.debug(LoggingLevel.FINE, 'The atom services is: ' + atomService);
			Feed myFeed = getFeed(path);
			if (myFeed == null) {
				System.debug(LoggingLevel.INFO, '\n\nWell, this feed is null...\n');
				return new List<ActivityNode>();
			}
			System.debug(LoggingLevel.INFO, '\n\nThis is the feed object: ' + myFeed + '\n');
			List<ActivityNode> entries = new List<ActivityNode>();
			
			List<AtomBase> myCommunityEntries = myFeed.getEntries();
			for (AtomBase currentEntry : myCommunityEntries ) {
				ActivityEntry ae = (ActivityEntry)currentEntry;
				ActivityNode currentCommunity = getNode(ae, common);
				entries.add(currentCommunity);
			}

			return entries;
		}
		catch (Exception e) {
			System.debug(LoggingLevel.ERROR, '\n\nERROR GETNODES(' + path + ', ' + common + ')\n' + e.getMessage());
		}
		return new List<ActivityNode>();
	}	
	
	protected List<ActivityNode> getActivityNodesBase(String path) {
		return getNodes(path, null);
	}
	protected List<ActivityNode> getNodesByParameter(String path, List<String> parameterList) {
		if (parameterList == null) {
			return new List<ActivityNode>();
		}
		List<ActivityNode> allNodes = new List<ActivityNode>();
		for (String parameter : parameterList) {
			List<ActivityNode> nodes = getNodes(path+parameter,parameter);
			addList(allNodes, nodes, parameter);
		}
		
		return allNodes;
	}
	 
	protected ActivityEntry createActivityEntry(String path, String title){
		AtomBase entry = new AtomBase();
		entry.setTitle(title);
		System.debug(entry);
		ActivityEntry response = (ActivityEntry)atomService.post(path,  entry);
		System.debug(LoggingLevel.INFO, '\n\nCreate Response: ' + response);
		return response;
	} 
	
	protected AtomBase createActivityEntry(String path, AtomBase activity){
		System.debug(activity);
		AtomBase response = atomService.post(path,  activity);
		System.debug(LoggingLevel.INFO, '\n\nCreate Response: ' + response);
		return response;
	} 

	protected AtomBase createActivityNode(String path, AtomBase node) {
		AtomBase response = atomService.post(path, node);
		System.debug(LoggingLevel.INFO, '\n\nCreate Response: ' + response);
		return response;
	}
	
	protected List<ActivityNode> addList(List<ActivityNode> baseList, List<ActivityNode> toBeAdded, String common){
		for (ActivityNode node: toBeAdded) {
			baseList.add(node);
		}
		return baseList;
	}
	

	public List<String> getAuthors(AtomBase entry) {
        List<String> authors = new List<String>();
        List<Person> eauth = entry.getAuthors();
        for (Person auth : eauth) {
        	authors.add(auth.name);
        }
        return authors;
	}

	public List<String> getContributors(AtomBase entry) {
		// get contributors
		List<String> contributors = new List<String>();
        List<Person> eauth = entry.getContributors();
        for (Person auth : eauth) {
        	contributors.add(auth.name);
        }
        return contributors;
	}
	
	protected ActivityNode getNode(ActivityEntry entry, String common){
		String blogId = '';
		try {
			blogId = entry.getId(); //.getPath();
		} catch (Exception e) {
		}

		// get authors

		// get content
		String content = entry.getContent().theContent.value;

		List<String> authors = getAuthors(entry);
		
		List<String> contributors = getContributors(entry);
		 
		//get date published 
		Datetime published = entry.getPublished().getDateTime(); 

		String link = getLink('alternate', entry);
		
		List<String> tags = getTags(entry, new String [0]);

		// get title
		String title = entry.getTitle();

		// get updated date
		Datetime updated = entry.getUpdated().getDateTime();
		
		String activityLink = entry.getActivityNodesUrl();
		 
		ActivityNode blog = new ActivityNode(blogId, 		// string
											 authors, 		// list
											 content,		// string
											 contributors,	// list
											 published,		// datetime
											 tags,			// list
											 title,			// string
											 updated,		// datetime
											 link,			// string
											 activityLink); // string
  		
		blog.addToCommonList(common);

		return blog;
	}

	

	public String getLink(String relType, AtomBase entry) {
		String link = '#';
		List<Link> links = entry.getLinks();
        for (Link aLink : links) {
            if (((relType != null && aLink.rel == relType) || aLink.rel == null)
            		&& aLink.lType == '1'){
                try {
                    link = aLink.href;
                    break;
                } catch (Exception e) {
                }
            }
        }
        return link;
	}
	
	
	public List<String> getTags(AtomBase entry, String[] excludeTags) {
		if (excludeTags == null) {
			excludeTags = new String[0];
		}
	    List<String> tags = new List<String>();
        return tags;
	}

	/*private boolean contains(String[] excludeTerms, String term) {
		for (String termToCheck : excludeTerms) {
			if (termToCheck.equals(term)) {
				return true;
			}
		}
		return false;
	}*/
}